// pages/scaner/index.js 
import Toast from '@vant/weapp/toast/toast';
import {
  get,
  post
} from '../../request/index'
const AUTH = require('../../utils/auth')
const {
  typeJson
} = require('../../utils/typejson')
const userInfo = JSON.parse(wx.getStorageSync('prolist'))

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    userId: ''
  },
  // 签到
  sign(e) {
    let data = e.currentTarget.dataset.item
    let parmes = {
      meetingId: data.id
    }
    post('/app/record/list', parmes).then(res => {
      let flag = true
      for (let i = 0; i < res.rows.length; i++) {
        console.log(res.rows[i].userId == this.data.userId);
        if (res.rows[i].userId == this.data.userId) {
          Toast.fail({ message: '重复签到', context: this });
          flag = false
        }
      }
      if (flag) {
        let time = new Date().toJSON().substring(0, 10) + ' ' + new Date().toTimeString().substring(0, 8)
        let parmes = {
          meetingId: data.id,
          userId: this.data.userId,
          signTime: time,
          mineId: 1
        }
        post('/app/record/add', parmes).then(res => {
          if (res.code == 200) {
            wx.showToast({
              title: '提交成功',
              duration: 3000,
              icon: 'success',
              mask: true
            })
            this.getList()
          }
        })
      }
    })

  },
  // 获取数据
  async getList() {
    let parmes = {
      userId: userInfo.userId,
      mineId: 1
    }
    let res = await get(`/app/meeting/list?pageNum=1&pageSize=10`, parmes)
    this.setData({
      dataList: res.rows
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: userInfo.userId
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})